package client;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import network.Client;
import network.Packet;
import network.Packet_Player;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;
//import org.lwjgl.util.glu.GLU;

import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;
import org.lwjgl.util.vector.Matrix4f;

import shared.MouseHook;
import shared.NinePatch;
import shared.Scene;
import shared.SceneManager;
import shared.Tools;
import shared.Vec2f;
import vmo.GameManager;

public class MainScene implements Scene
{
	GameManager p_gmanager;

	Main_Render m_render;
	Main_Sim m_sim;
	
	
//	NinePatch m_testpatch;

	
	public MainScene (Client client, int [] variables)
	{
		ColourMap.getInstance();
		m_sim=new Main_Sim(client);
		m_render=new Main_Render(variables,m_sim);
		m_render.MoveView(m_sim.m_player.m_pos);
		
	}
	
	public MainScene (Client client, int [] variables,Packet_Player player)
	{
		ColourMap.getInstance();	
		m_sim=new Main_Sim(client);
		m_render=new Main_Render(variables,m_sim);		
		m_sim.HandlePlayer(player);
		m_render.MoveView(m_sim.m_player.m_pos);
				
	}
	
	@Override
	public void Update(float dt) 
	{
		m_sim.Update(dt);
		if (m_sim.m_buildview)
		{
			m_render.Generate();
			m_sim.m_buildview=false;
		}
		
		//controls go here
		
		if (Keyboard.next())
		{
				
				boolean b=false;	
				//screen out the special keys 
				if (Keyboard.getEventKeyState())
				{		
					if (Keyboard.getEventKey()==Keyboard.KEY_HOME )
					{
						if (m_sim.m_player.m_layer<15)
						{
							m_sim.m_player.m_layer++;
							m_render.Generate();	
						}
					}
					if (Keyboard.getEventKey()==Keyboard.KEY_END )
					{
						if (m_sim.m_player.m_layer>0)
						{
							m_sim.m_player.m_layer--;
							m_render.Generate();
						}					
					}					
				}
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT) && m_sim.m_player.m_pos.x>0)
		{
			m_sim.m_player.m_pos.x-=0.1F;		
			m_render.MoveView(new Vec2f (-0.1F,0));
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && m_sim.m_player.m_pos.x<m_sim.m_width*16)
		{
			m_sim.m_player.m_pos.x+=0.1F;
			m_render.MoveView(new Vec2f (0.1F,0));			
		}	
		
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN) && m_sim.m_player.m_pos.y>0)
		{
			m_sim.m_player.m_pos.y-=0.1F;
			m_render.MoveView(new Vec2f (0,-0.1F));		
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_UP) && m_sim.m_player.m_pos.y<m_sim.m_height*16)
		{
			m_sim.m_player.m_pos.y+=0.1F;
			m_render.MoveView(new Vec2f (0,0.1F));		
		}		
		if (m_sim.ShiftNeeded())
		{
			m_render.Generate();
		}
	}
	





	@Override
	public void Start(MouseHook mouse) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void Draw() {
		// TODO Auto-generated method stub
		m_render.Draw();
	}
	@Override
	public void End() {
		// TODO Auto-generated method stub
		m_render.End();
		m_sim.End();
		
	}

}
